#include "gtk/gtk.h"
#include "widgets.h"
#include "generate_from_file.h"
#include "generate_single.h"

gboolean on_key_input(GtkWidget * caller, GdkEventKey * event, gpointer data) 
{
	// On escape pressed
	if      (event->keyval == GDK_KEY_Escape) // Window Escape
	{
		gtk_main_quit();
		return TRUE;
	}
	
	else if (caller == keyword_entry_0 && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter))
	{
		generate_from_file(caller, NULL);
		return TRUE;
	}

	else if (caller == service_entry && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter))
	{
		generate_single(caller, NULL);
		return TRUE;
	}	
	else if (caller == username_entry && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter))
	{
		generate_single(caller, NULL);
		return TRUE;
	}	
	else if (caller == keyword_entry_1 && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter))
	{
		generate_single(caller, NULL);
		return TRUE;
	}	
	return FALSE;
}

void connect_signals()
{
	g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_input), NULL);
	
	g_signal_connect(file_button, "clicked", G_CALLBACK(generate_from_file), NULL);
	g_signal_connect(keyword_entry_0, "key-press-event", G_CALLBACK(on_key_input), NULL);

	g_signal_connect(service_entry,   "key-press-event", G_CALLBACK(on_key_input), NULL);
	g_signal_connect(username_entry,  "key-press-event", G_CALLBACK(on_key_input), NULL);
	g_signal_connect(keyword_entry_1, "key-press-event", G_CALLBACK(on_key_input), NULL);
	g_signal_connect(single_button, "clicked", G_CALLBACK(generate_single), NULL);
}
